package com.inventory.controllers;

import com.inventory.entities.Customer;
import com.inventory.entities.CustomerDetails;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * CustomerControllerBean handles the CRUD functionality of the Customer relation
 * in the database using Customer entity class.
 * @author sithum
 */
@Stateless(mappedName = "CustomerController")
public class CustomerControllerBean implements CustomerControllerRemote {

    /*
     * Injects the specified Persistence unit to the EntityManager associated
     * with the current transaction.
     */
    @PersistenceContext(unitName = "SaicssPU")
    private EntityManager em;

    /**
     * Creates a set of new records in the customer relation.
     * @param java.util.List<Customer>
     */
    public void create(List<Customer> customerList, List<CustomerDetails> customerDetailsList) {
        for (Customer customer : customerList) {
            em.persist(customer);
            //em.persist(customerDetailsList.get(customerList.indexOf(customer)));
            em.flush();
        }
//        em.flush();
    }

    /**
     * Updates an existing set of records in the customer relation.
     * @param java.util.List<Customer>
     */
    public void edit(List<Customer> customerList, List<CustomerDetails> customerDetailsList) {
        for (Customer customer : customerList) {
            em.persist(em.merge(customer));
            //em.persist(em.merge(customerDetailsList.get(customerList.indexOf(customer))));
        }
    }

    /**
     * Removes a set of existing records from the customer relation in the database.
     * @param java.util.List<Customer> customerList
     */
    public void delete(List<Customer> customerList) {
        for (Customer customer : customerList) {
            em.remove(em.merge(customer));
//            for (CustomerDetails details : customer.getCustomerDetailsCollection()) {
//                em.remove(em.merge(details));
//            }
        }
    }

    /**
     * Finds a specific record using the customer name.
     * @param java.lang.String customerName
     * @return com.inventory.entities
     */
    public Customer findByCustomerName(String customerName) {
        Query findByName = em.createNamedQuery("Customer.findByCustomerName");
        return (Customer) findByName.setParameter("customerName", customerName).getSingleResult();
    }

    /**
     * Finds a specific record using the provided customer id.
     * @param com.inventory.entities.Customer customerId
     * @return com.inventory.entities.Customer
     */
    public Customer findByCustomerId(Customer customer) {
        return em.find(Customer.class, customer.getCustomerId());
    }

    /**
     * Returns all the records from the Customer relation.
     * @return java.util.List<Customer>
     */
    public List<Customer> findAll() {
        return em.createNamedQuery("Customer.findAll").getResultList();
    }

    /**
     * Returns the number of records in the Customer relation.
     * @return
     */
    public int count() {
        return Integer.valueOf(em.createNamedQuery("Customer.recordCount").getSingleResult().toString()).intValue();
    }
}
